package com.sztx.worker_monitor.web.handle;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sztx.se.core.tbschedule.component.ScheduleManager;
import com.sztx.se.core.tbschedule.config.ScheduleParameter;
import com.sztx.se.core.tbschedule.config.TaskRunningInfo;
import com.sztx.worker_monitor.common.StringUtils;
import com.sztx.worker_monitor.web.model.ResultStatus;
import com.sztx.worker_monitor.web.model.SearchListModel;
import com.sztx.worker_monitor.web.model.TaskRunInfoModel;
import com.sztx.worker_monitor.web.model.TbsModel;
import com.sztx.worker_monitor.web.page.TbsIndexPage;

@Component
public class TBSDealHandle {
	protected static final Logger logger = LoggerFactory.getLogger(TBSDealHandle.class);

	
	@Autowired(required = false)
	private ScheduleManager scheduleManager;
	
	private boolean isTaskExist(String scheduleName, String taskName, String strategyName){
		
		try{
			ScheduleParameter scheduleParameter = scheduleManager.getTask(scheduleName, taskName, strategyName);
			if(scheduleParameter != null) return true;
			
		}catch(Exception e){
			return false;
		}
		return false;
	}
	public TbsIndexPage getTbsIndexPage(){
		List<ScheduleParameter>  scheduleParameterList = null;
		try{
			 scheduleParameterList= scheduleManager.getAllTasks();
		}catch(Exception e){
			logger.error("getTbsIndexPage->"+e.toString());
			return new TbsIndexPage();
		}
		
		if(scheduleParameterList == null) return new TbsIndexPage();
		
		List<TbsModel> tbsList=new ArrayList<TbsModel>();
		
		for(ScheduleParameter scheduleParameter:scheduleParameterList){
			TbsModel tbsModel = new TbsModel(scheduleParameter);
			tbsList.add(tbsModel);
		}
		
		TbsIndexPage tbsIndexPage = new TbsIndexPage();
		tbsIndexPage.setTbsList(tbsList);
		
		return tbsIndexPage;
	}
	
public TbsIndexPage getTbsIndexPage(String keyTaskname, String keySchedname){
		
		logger.info("keyTaskname="+keyTaskname + ",keySchedname="+keySchedname);
		if(keyTaskname==null ||"".equals(keyTaskname)){
			keyTaskname = "";
		}
		
		if(keySchedname==null ||"".equals(keySchedname)){
			keySchedname = "";
		}
		if(keyTaskname.length() == 0 && keySchedname.length() == 0)
			return getTbsIndexPage();
		List<ScheduleParameter>  scheduleParameterList= null;
		try{
			scheduleParameterList= scheduleManager.getAllTasks();
		}catch(Exception e){
			logger.error(e.toString());
			return new TbsIndexPage();
		}
		
		if(scheduleParameterList == null) return new TbsIndexPage();
		
		List<TbsModel> tbsList=new ArrayList<TbsModel>();
		
		for(ScheduleParameter scheduleParameter:scheduleParameterList){
			
			if(keyTaskname.length() != 0 && keySchedname.length() != 0){
				if(scheduleParameter.getTaskName().indexOf(keyTaskname) != -1 &&
						scheduleParameter.getScheduleName().indexOf(keySchedname) != -1){
					TbsModel tbsModel = new TbsModel(scheduleParameter);
					tbsList.add(tbsModel);
				}
			}else if(keyTaskname.length() != 0){
				if(scheduleParameter.getTaskName().indexOf(keyTaskname) != -1 ){
					TbsModel tbsModel = new TbsModel(scheduleParameter);
					tbsList.add(tbsModel);
				}
				
			}else{
				if(scheduleParameter.getScheduleName().indexOf(keySchedname) != -1){
					TbsModel tbsModel = new TbsModel(scheduleParameter);
					tbsList.add(tbsModel);
				}
				
			}
		}
		
		TbsIndexPage tbsIndexPage = new TbsIndexPage();
		tbsIndexPage.setTbsList(tbsList);
		tbsIndexPage.setKeySchedname(keySchedname);
		tbsIndexPage.setKeyTaskname(keyTaskname);
		return tbsIndexPage;
	}

	public ResultStatus addNewJob(
			  	String 	scheduleName,
				String 	strategyName,
				String 	taskName,
				String 	taskBean,
				Integer fetchDataNumber,
				Integer executeNumber,
				
				String 	taskItems,
				String	startTime,
				String 	endTime,
				String 	extraInfo){
		
		
		logger.info("addNewJob=>scheduleName="+scheduleName + 
					",strategyName="+strategyName+
					",taskName="+taskName+
					",taskBean="+taskBean+
					",fetchDataNumber="+fetchDataNumber+
					",executeNumber="+executeNumber+
					",taskItems="+taskItems+
					",startTime="+startTime+
					",endTime="+endTime+
					",extraInfo="+extraInfo);
		
		
		if(scheduleName == null ||scheduleName.equals("")){
			
			return new ResultStatus("调度器名称不能为空！");
		}
		
		if(strategyName == null ||strategyName.equals("")){
			
			return new ResultStatus("策略名称不能为空！");
		}

		if(taskName == null ||taskName.equals("")){
			
			return new ResultStatus("任务名称不能为空！");
		}
		
		if(taskBean == null ||taskBean.equals("")){
			
			return new ResultStatus("任务处理bean不能为空！");
		}
		
		if(taskItems == null ||taskItems.equals("")){
			
			return new ResultStatus("任务项不能为空！");
		}

		
		if(fetchDataNumber == null){
			return new ResultStatus("每次获取数据量不能为空！");
		}
		
		if(executeNumber == null){
			return new ResultStatus("每次执行数量不能为空！");
		}
		
		if(isTaskExist(scheduleName, taskName, strategyName)){
			return new ResultStatus("该任务已经存在！");
		}
		
		List<String> taskItemList =StringUtils.splitString(taskItems, ";");
		for(int i =0; i < taskItemList.size() ; i++){
			String temp = com.sztx.worker_monitor.common.StringUtil.trimString(taskItemList.get(i), " ");
			if(temp == null || temp.length() == 0){
				taskItemList.remove(i);
				i--;
			}
		}
		try{
			if(!scheduleManager.saveTask(
					scheduleName,
					taskName, 
					taskBean,
					strategyName,
					fetchDataNumber,
					executeNumber,
					taskItemList,
					ScheduleParameter.SLEEP_MODEL,
					startTime,
					endTime,
					extraInfo)){
				return new ResultStatus("创建任务失败，内部错误");
			}
		}catch(Exception e){
			logger.error(e.toString());
			return new ResultStatus("创建任务失败，内部错误");
		}
		
		return new ResultStatus();
	}	
	
	public ResultStatus delJob(
		  	String 	scheduleName,
			String 	strategyName,
			String 	taskName
			){
		
		logger.info("delJob=>scheduleName="+scheduleName + 
				",strategyName="+strategyName+
				",taskName="+taskName);
		
		
		if(scheduleName == null ||scheduleName.equals("")){
			
			return new ResultStatus("调度器名称不能为空！");
		}
		
		if(strategyName == null ||strategyName.equals("")){
			
			return new ResultStatus("策略名称不能为空！");
		}
	
		if(taskName == null ||taskName.equals("")){
			
			return new ResultStatus("任务名称不能为空！");
		}
		
		if(!isTaskExist(scheduleName, taskName, strategyName)){
			return new ResultStatus("该任务不存在！");
		}
		
		
		try{
			if(!scheduleManager.deleteTask(
					scheduleName,
					taskName, 
					strategyName
					)){
				return new ResultStatus("删除任务失败，内部错误");
			}
		}catch(Exception e){
			logger.error(e.toString());
			return new ResultStatus("删除任务失败，内部错误");
		}
		return new ResultStatus();
	}
	
	public ResultStatus pauseJob(
		  	String 	scheduleName,
			String 	strategyName,
			String 	taskName
			){
		
		logger.info("pauseJob=>scheduleName="+scheduleName + 
				",strategyName="+strategyName+
				",taskName="+taskName);
		
		
		if(scheduleName == null ||scheduleName.equals("")){
			
			return new ResultStatus("调度器名称不能为空！");
		}
		
		if(strategyName == null ||strategyName.equals("")){
			
			return new ResultStatus("策略名称不能为空！");
		}
	
		if(taskName == null ||taskName.equals("")){
			
			return new ResultStatus("任务名称不能为空！");
		}
		
		if(!isTaskExist(scheduleName, taskName, strategyName)){
			return new ResultStatus("该任务不存在！");
		}
		
		
		try{
			if(!scheduleManager.pauseTask(
					scheduleName,
					taskName, 
					strategyName
					)){
				return new ResultStatus("暂停任务失败，内部错误");
			}
		}catch(Exception e){
			logger.error(e.toString());
			return new ResultStatus("暂停任务失败，内部错误");
		}
		return new ResultStatus();
	}
	
	
	public ResultStatus resumeJob(
		  	String 	scheduleName,
			String 	strategyName,
			String 	taskName
			){
		
		logger.info("resumeJob=>scheduleName="+scheduleName + 
				",strategyName="+strategyName+
				",taskName="+taskName);
		
		
		if(scheduleName == null ||scheduleName.equals("")){
			
			return new ResultStatus("调度器名称不能为空！");
		}
		
		if(strategyName == null ||strategyName.equals("")){
			
			return new ResultStatus("策略名称不能为空！");
		}
	
		if(taskName == null ||taskName.equals("")){
			
			return new ResultStatus("任务名称不能为空！");
		}
		
		if(!isTaskExist(scheduleName, taskName, strategyName)){
			return new ResultStatus("该任务不存在！");
		}
		
		
		try{
			if(!scheduleManager.resumeTask(
					scheduleName,
					taskName, 
					strategyName
					)){
				return new ResultStatus("启动任务失败，内部错误");
			}
		}catch(Exception e){
			logger.error(e.toString());
			return new ResultStatus("启动任务失败，内部错误");
		}
		return new ResultStatus();
	}
	
		
	public ResultStatus updateJob(
		  	String 	scheduleName,
			String 	strategyName,
			String 	taskName,
			String 	taskBean,
			Integer fetchDataNumber,
			Integer executeNumber,
			
			String 	taskItems,
			String	startTime,
			String 	endTime,
			String 	extraInfo){
		logger.info("updateJob=>scheduleName="+scheduleName + 
				",strategyName="+strategyName+
				",taskName="+taskName+
				",taskBean="+taskBean+
				",fetchDataNumber="+fetchDataNumber+
				",executeNumber="+executeNumber+
				",taskItems="+taskItems+
				",startTime="+startTime+
				",endTime="+endTime+
				",extraInfo="+extraInfo);
		
	if(scheduleName == null ||scheduleName.equals("")){
		
		return new ResultStatus("调度器名称不能为空！");
	}
	
	if(strategyName == null ||strategyName.equals("")){
		
		return new ResultStatus("策略名称不能为空！");
	}

	if(taskName == null ||taskName.equals("")){
		
		return new ResultStatus("任务名称不能为空！");
	}
	
	if(taskBean == null ||taskBean.equals("")){
		
		return new ResultStatus("任务处理bean不能为空！");
	}
	
	if(taskItems == null ||taskItems.equals("")){
		
		return new ResultStatus("任务项不能为空！");
	}

	
	if(fetchDataNumber == null){
		return new ResultStatus("每次获取数据量不能为空！");
	}
	
	if(executeNumber == null){
		return new ResultStatus("每次执行数量不能为空！");
	}
	
	if(!isTaskExist(scheduleName, taskName, strategyName)){
		return new ResultStatus("该任务不存在！");
	}
	
	List<String> taskItemList =StringUtils.splitString(taskItems, ";");
	for(int i =0; i < taskItemList.size() ; i++){
		String temp = com.sztx.worker_monitor.common.StringUtil.trimString(taskItemList.get(i), " ");
		if(temp == null || temp.length() == 0){
			taskItemList.remove(i);
			i--;
		}
	}
	
	
	try{
		if(!scheduleManager.updateTask(
				scheduleName,
				taskName, 
				taskBean,
				strategyName,
				fetchDataNumber,
				executeNumber,
				taskItemList,
				ScheduleParameter.SLEEP_MODEL,
				startTime,
				endTime,
				extraInfo)){
			return new ResultStatus("修改任务失败，内部错误");
		}
	}catch(Exception e){
		logger.error(e.toString());
		return new ResultStatus("修改任务失败，内部错误");
	}
	
	return new ResultStatus();
}	
	
	
	
	
	public SearchListModel getSearchList(){

		TbsIndexPage tbsIndexPage =getTbsIndexPage();
		 
		return new SearchListModel(tbsIndexPage); 

	}
	
	public List<TaskRunInfoModel> getTaskRuningInfo(
			String 	scheduleName,
			String 	strategyName,
			String 	taskName){
		
		logger.info("getTaskRuningInfo=>scheduleName="+scheduleName + 
				",strategyName="+strategyName+
				",taskName="+taskName);
		
		List<TaskRunInfoModel> taskRunInfoList = new ArrayList<TaskRunInfoModel>();
		
		if(scheduleName == null || scheduleName.length() == 0){
			 return taskRunInfoList;	
		}
		if(strategyName == null || strategyName.length() == 0){
			 return taskRunInfoList;	
		}
		if(taskName == null || taskName.length() == 0){
			 return taskRunInfoList;	
		}
		
		try{
			List<TaskRunningInfo> taskRunningInfos = 
					scheduleManager.getTaskRunningInfoList(scheduleName, taskName, strategyName);
			if(taskRunningInfos == null) return taskRunInfoList;
			
			for(TaskRunningInfo taskRunningInfo : taskRunningInfos){
				TaskRunInfoModel TaskRunInfoModel = new TaskRunInfoModel(taskRunningInfo);
				taskRunInfoList.add(TaskRunInfoModel);
			}
		}catch(Exception e){
			logger.error(e.toString());
		}
	
		return taskRunInfoList;
		
	}
	
	
}
